﻿using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SimpleCqrs;
using SimpleCqrs.Commanding;

namespace Domain.Engine.Tests
{
  public class DomainTestBase
  {
    private readonly Game _game = new Game();
    private readonly ReadModel _readModel = new ReadModel();
    private ICommandBus _commandBus;
    private IUnityContainer _container;
    private ISimpleCqrsRuntime _runtime;

    protected ICommandBus CommandBus
    {
      get { return _commandBus ?? (_commandBus = _container.Resolve<ICommandBus>()); }
    }

    public Game Game
    {
      get { return _game; }
    }

    public ReadModel ReadModel
    {
      get { return _readModel; }
    }

    [TestInitialize]
    public void Initialize()
    {
      _container = new UnityContainer();
      ISimpleCqrsRuntime testRuntime = new TestRuntime(_container);
      testRuntime.Start();
      _runtime = testRuntime;
      _runtime.ServiceLocator.Register(_readModel);
      _runtime.ServiceLocator.Register(_game);
    }

    [TestCleanup]
    public void Cleanup()
    {
      _runtime.Shutdown();
    }
  }
}